package com.qyhl.webtv.module_news.news.collect;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/12/5.
 */

public class CollectModel implements CollectContract.CollectModel {
    private CollectPresenter mPresenter;

    public CollectModel(CollectPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取收藏
     *
     * @param pageId
     */
    @Override
    public void getData(final String pageId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(NewsUrl.GET_NEWS_COLLECTION)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("ID", pageId)
                .params("userName", userName)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if ("0".equals(pageId)) {
                                mPresenter.setError(2, "暂无收藏，快去收藏吧！");
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        } else {
                            if ("0".equals(pageId)) {
                                mPresenter.setError(4, "解析出错，加载失败！");
                            } else {
                                mPresenter.setError(5, "解析出错，加载失败！");
                            }

                        }
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if ("0".equals(pageId)) {
                            if (newsBeans != null && newsBeans.size() > 0) {
                                mPresenter.setDatas(newsBeans, false);
                            } else {
                                mPresenter.setError(2, "暂无收藏，快去收藏吧！");
                            }
                        } else {
                            if (newsBeans != null && newsBeans.size() > 0) {
                                mPresenter.setDatas(newsBeans, true);
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        }
                    }
                });

    }

    /**
     * 取消收藏
     *
     * @param newsId
     */
    @Override
    public void cancelCollect(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "deleteCollectArticle")
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("articleID", newsId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelCollectError("网络异常，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.cancelCollectSuccess();
                        } else {
                            mPresenter.cancelCollectError("取消失败，请稍后重试！");
                        }
                    }
                });
    }
}
